package com.kool.kadmin.mqtt.constant;

import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : luyu
 * @date :2021/3/26 14:10
 */
@Getter
public enum ActionEnum {
    SUBSCRIBE(0, "subscribe", "订阅"),
    PUBLISH(1, "publish", "发布");

    /**
     * 权限位图编码下标位
     */
    Integer bitIndex;
    /**
     * 操作
     */
    String action;
    String desc;

    ActionEnum(Integer bitIndex, String action, String desc) {
        this.bitIndex = bitIndex;
        this.action = action;
        this.desc = desc;
    }

    public static Map<String, ActionEnum> actionMap = Arrays.stream(ActionEnum.values()).collect(Collectors.toMap(ActionEnum::getAction, item -> item));

    /**
     * 根据操作查询权限位图编码下标位
     *
     * @param action
     * @return
     */
    public static Integer getBitIndex(String action) {
        ActionEnum actionEnum = actionMap.get(action);
        if (actionEnum == null) {
            return null;
        } else
            return actionEnum.getBitIndex();
    }

    public static Map<Integer, ActionEnum> bitIndexMap = Arrays.stream(ActionEnum.values()).collect(Collectors.toMap(ActionEnum::getBitIndex, item -> item));

    /**
     * 根据下标查询操作
     *
     * @param bitIndex
     * @return
     */
    public static String getAction(Integer bitIndex) {
        ActionEnum actionEnum = bitIndexMap.get(bitIndex);
        if (actionEnum == null) {
            return null;
        } else
            return actionEnum.getAction();
    }
}
